/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductInputPane.java
 *
 * Created on 24-jul-2010, 17:29:10
 */

package facturasapp;

import com.jhlabs.image.Histogram;
import facturasapp.data.DataManager;
import facturasapp.data.StoreManager;
import facturasapp.events.FacturaEvent;
import facturasapp.events.EventManager;
import facturasapp.events.SearchAdapter;
import facturasapp.events.SearchEvent;
import facturasapp.events.SearchListener;
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import storeDataModel.Invoice;
import storeDataModel.Product;
import storeDataModel.ProductRelation;

/**
 *
 * @author CiberXtrem
 */
public class ItemSearchPreviewPane extends javax.swing.JPanel {

    /** Creates new form ProductInputPane */
    public ItemSearchPreviewPane(Invoice p_invoice) {
        invoice = p_invoice;
        initComponents();
        initCustomComponentes();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        facturaField = new javax.swing.JTextField();
        fechaField = new javax.swing.JTextField();
        matriculaField = new javax.swing.JTextField();
        modeloField = new javax.swing.JTextField();
        addProductoCestaButton = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(facturasapp.FacturasApp.class).getContext().getResourceMap(ItemSearchPreviewPane.class);
        facturaField.setText(resourceMap.getString("facturaField.text")); // NOI18N
        facturaField.setName("facturaField"); // NOI18N
        facturaField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                facturaFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                facturaFieldFocusLost(evt);
            }
        });

        fechaField.setText(resourceMap.getString("fechaField.text")); // NOI18N
        fechaField.setName("fechaField"); // NOI18N
        fechaField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                fechaFieldFocusGained(evt);
            }
        });

        matriculaField.setText(resourceMap.getString("matriculaField.text")); // NOI18N
        matriculaField.setName("matriculaField"); // NOI18N
        matriculaField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                matriculaFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                matriculaFieldFocusLost(evt);
            }
        });

        modeloField.setText(resourceMap.getString("modeloField.text")); // NOI18N
        modeloField.setName("modeloField"); // NOI18N
        modeloField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                modeloFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                modeloFieldFocusLost(evt);
            }
        });

        addProductoCestaButton.setIcon(resourceMap.getIcon("addProductoCestaButton.icon")); // NOI18N
        addProductoCestaButton.setText(resourceMap.getString("addProductoCestaButton.text")); // NOI18N
        addProductoCestaButton.setName("addProductoCestaButton"); // NOI18N
        addProductoCestaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addProductoCestaButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(facturaField, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(fechaField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(matriculaField, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(modeloField, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(addProductoCestaButton, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(fechaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(facturaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(matriculaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(modeloField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(addProductoCestaButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void initCustomComponentes(){
        facturaField.setText(String.valueOf(invoice.getId()));

        Date date = invoice.getFecha();
        String dateFormat = "dd/MM/yyyy HH:mm:ss";
        SimpleDateFormat dFormat = new SimpleDateFormat(dateFormat);
        fechaField.setText(dFormat.format(date));
        matriculaField.setText(invoice.getMatricula());
        modeloField.setText(invoice.getModelo());

        EventManager.getInstance().addSearchListener(new SearchAdapter() {

            public void invoiceUnselected(SearchEvent e) {
                org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(facturasapp.FacturasApp.class).getContext().getResourceMap(ItemSearchPreviewPane.class);
                addProductoCestaButton.setIcon(resourceMap.getIcon("addProductoCestaButton.icon")); // NOI18N
                //setBackground(Color.gray);
            }
        });
    }
    
    private void addProductoCestaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addProductoCestaButtonActionPerformed
        //Notify to reset values
        EventManager.getInstance().fireSearchEvent(new SearchEvent(invoice), EventManager.SEARCH_INVOICE_UNSELECTED);

        //this.setBackground(Color.DARK_GRAY);
      org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(facturasapp.FacturasApp.class).getContext().getResourceMap(ItemSearchPreviewPane.class);
      addProductoCestaButton.setIcon(resourceMap.getIcon("addProductoCestaButton_selected.icon")); // NOI18N

      EventManager.getInstance().fireSearchEvent(new SearchEvent(invoice), EventManager.SEARCH_INVOICE_SELECTED);
    }//GEN-LAST:event_addProductoCestaButtonActionPerformed

    private void facturaFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_facturaFieldFocusLost

    }//GEN-LAST:event_facturaFieldFocusLost

    private void matriculaFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_matriculaFieldFocusLost

    }//GEN-LAST:event_matriculaFieldFocusLost

    private void modeloFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_modeloFieldFocusLost

    }//GEN-LAST:event_modeloFieldFocusLost

    private void matriculaFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_matriculaFieldFocusGained

    }//GEN-LAST:event_matriculaFieldFocusGained

    private void facturaFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_facturaFieldFocusGained

    }//GEN-LAST:event_facturaFieldFocusGained

    private void fechaFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_fechaFieldFocusGained

    }//GEN-LAST:event_fechaFieldFocusGained

    private void modeloFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_modeloFieldFocusGained

    }//GEN-LAST:event_modeloFieldFocusGained


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addProductoCestaButton;
    private javax.swing.JTextField facturaField;
    private javax.swing.JTextField fechaField;
    private javax.swing.JTextField matriculaField;
    private javax.swing.JTextField modeloField;
    // End of variables declaration//GEN-END:variables

    //Custom Data Variables
    Invoice invoice=null;
    //End Custom Data Variables
}
